package com.sixkey.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixkey.annotation.OperationLog;
import com.sixkey.entity.Notice;
import com.sixkey.entity.User;
import com.sixkey.entity.vo.NoticeVo;
import com.sixkey.response.Response;
import com.sixkey.service.INoticeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 公告表 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-09-30
 */
@Slf4j
@Validated
@CrossOrigin
@RestController
@RequestMapping("/notice")
@RequiredArgsConstructor
public class NoticeController {

    private final INoticeService noticeService;

    /**
     * 公告管理分页查询
     * @param
     * @param pageSize
     * @param title
     * @return
     */
    @GetMapping("/list/{pageNo}/{pageSize}")
    public Response page(@PathVariable("pageNo") int pageNo,
                         @PathVariable("pageSize") int pageSize,
                         String title){
        log.info("公告分页数据请求进来了....");
        Page<Notice> noticePage = noticeService.noticePage(pageNo,pageSize,title);
        return Response.success(noticePage);
    }

    /**
     * 保存新建公告
     * @param
     * @return
     */
    @PostMapping("/save")
    @OperationLog("新建公告")
    public Response saveWithNotice(@RequestBody @Valid Notice notice){
        log.info("保存新建公告请求进来~");
        noticeService.save(notice);
        return Response.success();
    }

    /**
     * 删除公告
     * @param
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @OperationLog("删除公告")
    public Response remove(@PathVariable("id") List<Integer> id){
        log.info("删除公告请求进来！");
        noticeService.removeWithType(id);
        return Response.success();
    }

    /**
     * 修改公告
     * @param
     * @return
     */
    @PutMapping("/update")
    @OperationLog("修改公告")
    public Response update(@RequestBody @Valid Notice notice){
        log.info("修改公告请求进来~");
        noticeService.updateNotice(notice);
        return Response.success();
    }

    /**
     * 修改公告状态
     * @param
     * @return
     */
    @PutMapping("/status/{id}/{status}")
    @OperationLog("修改公告状态")
    public Response updateStatus(@PathVariable("id") Integer id,
                                 @PathVariable("status") Integer status){
        log.info("修改公告状态请求进来");
        noticeService.updateStatus(id,status);
        return Response.success();
    }

    /**
     * 公告数据回显
     * @param
     * @return
     */
    @GetMapping("/echo/{id}")
    public Response getUserInfo(@PathVariable("id") Integer id){
        log.info("户数据回显请求进来");
        Notice notice = noticeService.getById(id);
        return Response.success(notice);
    }

    /**
     * 批量发布公告
     * @param
     * @return
     */
    @PutMapping("/publish/{id}")
    @OperationLog("批量发布公告")
    public Response publishBatch(@PathVariable("id") List<Integer> id){
        log.info("批量发布公告请求进来！");
        noticeService.publishBatch(id);
        return Response.success();
    }

    /**
     * 批量撤销公告
     * @param
     * @return
     */
    @PutMapping("/unDoStatus/{id}")
    @OperationLog("批量撤销公告")
    public Response unDoStatus(@PathVariable("id") List<Integer> id){
        log.info("批量撤销公告请求进来！");
        noticeService.unDoStatus(id);
        return Response.success();
    }
}
